package member.service;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;

import member.action.form.MemberActionForm;
import member.dao.MemberDAO;
import member.vo.MemberVO;

public class MemberCRUDService {
	private MemberDAO mdao;
	
	public void setMdao(MemberDAO mdao){
		this.mdao = mdao;
	}

	public void insertMember(ActionForm form){
		MemberActionForm mform = (MemberActionForm)form;
		HashMap mmap = new HashMap();
		mmap.put("name", mform.getName());
		mmap.put("id", mform.getId());
		mmap.put("password", mform.getPassword());
		mmap.put("deptid", mform.getDeptid());
		mmap.put("position", mform.getPosition());
		mmap.put("address", mform.getAddress());
		mmap.put("phone", mform.getPhone());
		mmap.put("hphone", mform.getHphone());
		mmap.put("email", mform.getEmail());
		mmap.put("detail", mform.getDetail());
		mdao.registMember(mmap);
	}

	public String deleteMember(String id) {
		String result = "delete_success";
		int cnt = mdao.deleteMember(id);
		if(cnt!=1){
			result = "delete_fail";		
		}
		return result;
	}

	public HashMap showMember(String id) {
		HashMap mmap = mdao.showMember(id);
		return mmap;		
	}

	public List showAllMember() {
		List mlist = mdao.showAllMember();		
		return mlist;
	}
	
	
	public String modifyMember(ActionForm form){
		MemberActionForm mform = (MemberActionForm)form;
		HashMap mmap = new HashMap();
		mmap.put("id", mform.getId());
		mmap.put("name", mform.getName());
		mmap.put("password", mform.getPassword());
		mmap.put("deptid", mform.getDeptid());
		mmap.put("position", mform.getPosition());
		mmap.put("address", mform.getAddress());
		mmap.put("phone", mform.getPhone());
		mmap.put("hphone", mform.getHphone());
		mmap.put("email", mform.getEmail());
		mmap.put("detail", mform.getDetail());
		
		int cnt = mdao.modifyMember(mmap);
		String result = "modify_success";
		if(cnt!=1){
			result = "modify_fail";		
		}
		return result;
	}
	
//add by KO.
	public int checkMemberByDeptId(String deptId) {
		int mid = mdao.checkMemberByDeptId(deptId);
		return mid;		
	}
}
